public class Text4 {
    //1658.将x减到0的最小操作数
    public int minOperations(int[] nums, int x) {
        int sum1 = 0;
        for(int a :nums) sum1+=a;

        int sum2 = sum1 - x;
        if(sum2 < 0)
            return -1;

        int left = 0, right = 0, ret = -1;
        int tmp = 0;
        while(right < nums.length){
            tmp += nums[right];
            while(tmp > sum2){
                tmp -= nums[left++];
            }
            if (tmp == sum2)
                ret = Math.max(ret , right - left + 1);
            right++;
        }
        if(ret == -1) return -1;
        else return nums.length - ret;
    }
}
